package com.example.module_word.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bdy.voice.util.VoicecompoundTool;
import com.example.module_word.R;
import com.example.module_word.adapter.LexiconRvAdapter;
import com.example.module_word.data.DanCiData;
import com.example.module_word.databinding.ActivityLexiconBinding;
import com.example.module_word.room.Danci;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.utils.AdUtil;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;
import java.util.List;

public class LexiconActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityLexiconBinding binding;
    private TextView currentTv;
    private String kind;
    private int jrdc;
    private VoicecompoundTool bdy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLexiconBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        MyStatusBarUtils.setStatusBar(this,"#00fff000");
        bindListener();
        bdy = new VoicecompoundTool(this);
        bdy.initbdyKey("115791963", "com.wyf.lyf_voice");
        initView();
    }

    private void initView() {
        tabChange(binding.lexiconJrdc);
        binding.lexiconRv.setLayoutManager(new LinearLayoutManager(this));
        kind = getIntent().getStringExtra("kind");
        try {
            jrdc = Integer.parseInt(getIntent().getStringExtra("jrdc"));
        }catch (Exception e){
            jrdc = 0;
        }

        initAdapter(DanCiData.getDanCiByKind(kind).subList(0,jrdc));
    }
    //切换标签
    private void tabChange(TextView tv){
        tv.setSelected(true);
        tv.setTextColor(Color.parseColor("#ff313337"));
        currentTv = tv;
    }

    private void initAdapter(List<Danci> list){
        LexiconRvAdapter lexiconRvAdapter = new LexiconRvAdapter(list);
        binding.lexiconRv.setAdapter(lexiconRvAdapter);
        lexiconRvAdapter.setOnListClickListener(new OnListClickListener<String>() {
            @Override
            public void itemClick(int position, String data) {
                AdUtil.clickAdAndPayWithIncentiveMode(LexiconActivity.this,1,1,() -> {
                    bdy.speak(data);
                    return null;
                });
            }
        });
    }

    private void bindListener() {
        binding.lexiconBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        binding.lexiconJrdc.setOnClickListener(this);
        binding.lexiconYzwdc.setOnClickListener(this);
        binding.lexiconDclb.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        currentTv.setSelected(false);
        currentTv.setTextColor(Color.parseColor("#ABB0BF"));
        int id = v.getId();
        if (id == R.id.lexicon_jrdc){
            tabChange(binding.lexiconJrdc);
            initAdapter(DanCiData.getDanCiByKind(kind).subList(0,jrdc));
        }else if (id == R.id.lexicon_yzwdc){
            tabChange(binding.lexiconYzwdc);
            LexiconRvAdapter lexiconRvAdapter = new LexiconRvAdapter(DanCiData.getMyWord(),1);
            binding.lexiconRv.setAdapter(lexiconRvAdapter);
        }else if (id == R.id.lexicon_dclb){
            tabChange(binding.lexiconDclb);
            initAdapter(DanCiData.getDanCiByKind(kind));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bdy.release();
    }
}